/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ccxc.question.user.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ccxc.question.commons.utils.IdAndTimeUtil;
import com.ccxc.question.user.dto.User;
import com.ccxc.question.user.vo.OneValueVO;
import com.ccxc.question.user.vo.WUser;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * <p>根据opengid查询是否该微信用户注册过</p>
     *
     * @param openid
     * @Title: findUser
     * @version:V0.1
     * @return:List<User>
     */
    public boolean findUser(String openid) {
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        String sql = "SELECT u.`ID`,u.`OPEN_ID`,u.`HEAD_PORTRAIT`,u.`NICKNAME`,u.`COUNTRY`,u.`PROVINCES`,u.`CITY`,u.`CREATED_DATE`,u.`TOKEN` FROM`t_user_information` AS u WHERE u.`OPEN_ID` = ?";
        List<User> list = jdbcTemplate.query(sql, rowMapper, openid);
        if (0 == list.size()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * <p>注册用户</p>
     *
     * @param user
     * @param openid
     * @return
     * @Title: reUser
     * @version:V0.1
     * @return:int
     */
    public int reUser(WUser user, String openid) {
        String sql = "INSERT INTO  `t_user_information` (`OPEN_ID`,`HEAD_PORTRAIT`,`NICKNAME`,`COUNTRY`,`PROVINCES`,`CITY`,`CREATED_DATE`) VALUES (?,?,?,?,?,?,?)";
        int number = jdbcTemplate.update(sql, openid, user.getHeadPortrait(), user.getNickname(), user.getCountry(), user.getProvince(), user.getCity(), IdAndTimeUtil.getNewDate());
        log.info("==========注册用户========" + String.valueOf(number));
        return number;
    }

    /**
     * <p>获取用户数据</p>
     *
     * @param openid
     * @return
     * @Title: findByOpenId
     * @version:V0.1
     * @return:User
     */
    public User findByOpenId(String openid) {
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        String sql = "SELECT u.ID,u.`OPEN_ID`,u.`HEAD_PORTRAIT`,u.`NICKNAME`,u.`COUNTRY`,u.`PROVINCES`,u.`CITY`,u.`CREATED_DATE`,u.`TOKEN` FROM`t_user_information` AS u WHERE u.`OPEN_ID` = ?";
        return jdbcTemplate.queryForObject(sql, rowMapper, openid);
    }

    /**
     * <p>用户注册人脸获得token写入数据库</p>
     *
     * @param oneValueVO
     * @return
     * @Title: addToken
     * @version:V0.1
     * @return:int
     */
    public int addToken(OneValueVO oneValueVO) {
        String sql = "UPDATE  t_user_information SET `TOKEN` = ? WHERE `OPEN_ID` = ?";
        int number = jdbcTemplate.update(sql, oneValueVO.getToken(), oneValueVO.getOpenid());
        log.info("==========写入token成功========" + String.valueOf(number));
        return number;
    }

    public String findUser2(String openid) {
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
        String sql = "SELECT u.`ID`,u.`OPEN_ID`,u.`HEAD_PORTRAIT`,u.`NICKNAME`,u.`COUNTRY`,u.`PROVINCES`,u.`CITY`,u.`CREATED_DATE`,u.`TOKEN` FROM`t_user_information` AS u WHERE u.`OPEN_ID` = ?";
        List<User> list = jdbcTemplate.query(sql, rowMapper, openid);
        if (0 != list.size()) {
            return list.get(0).getId();
        } else {
            return null;
        }
    }

}
